package com.zz.cc.owner.app;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;

import com.zz.cc.owner.R;
import com.zz.cc.owner.TitleBaseActivity;
import com.zz.cc.owner.data.Comment;
import com.zz.cc.owner.data.Store;
import com.zz.common.utils.DateTimeUtil;
import com.zz.common.widget.ZListView;

public class DetailActivity extends TitleBaseActivity implements OnClickListener, OnItemClickListener {
	public static final String STORE_DETAIL = "store_detail";
	private View mContentView;
	private TextView mBasicInfoTextView;
	private TextView mAddressTextView;
	private TextView mPhoneNumberTextView;
	private TextView mIntroduceTextView;
	
	private ZListView mListView;
	private CommentAdapter mAdapter;
	
	private View mBtnGroup;
	private Button mServiceBtn;
	private Button mGoThereBtn;
	private Button mCommentBtn;
	private Button mShareBtn;
	private Store mStore;
	private final List<Comment> mComments = new ArrayList<Comment>();
	private RatingBar mRatingBar;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mStore = (Store)getIntent().getSerializableExtra(STORE_DETAIL);
		setContentView(R.layout.activity_detail);
		initUI();
		fillView();
		refresh();
	}
	
	private void initUI() {
		mContentView = LayoutInflater.from(this).inflate(R.layout.layout_detail_content, null);
		initContentView();
		
		mListView = (ZListView) findViewById(R.id.detail_list_view);
		
		mBtnGroup = findViewById(R.id.detail_btn_group);
		mRatingBar = (RatingBar)mContentView.findViewById(R.id.ratingbar_default);
		mRatingBar.setRating(mStore.score);
		initBtnGroup();
		
	}
	
	private void initContentView() {
		mBasicInfoTextView = (TextView) mContentView.findViewById(R.id.detail_basic_info);
		mAddressTextView = (TextView) mContentView.findViewById(R.id.detail_address);
		mPhoneNumberTextView = (TextView) mContentView.findViewById(R.id.detail_phone_number);
		mIntroduceTextView = (TextView) mContentView.findViewById(R.id.detail_introduce);
		
		
	}
	
	private void initBtnGroup() {
		mServiceBtn = (Button) mBtnGroup.findViewById(R.id.detail_service);
		mGoThereBtn = (Button) mBtnGroup.findViewById(R.id.detail_go_there);
		mCommentBtn = (Button) mBtnGroup.findViewById(R.id.detail_comment);
		mShareBtn = (Button) mBtnGroup.findViewById(R.id.detail_share);
	}
	
	private void fillView() {
		setCurrentTitle(R.string.detail_title);
		fillContentView();
		fillBtnGroup();
		
		mListView.addHeaderView(mContentView);
		mAdapter = new CommentAdapter();
		mListView.setAdapter(mAdapter);
	}
	
	private void fillContentView() {
		String basicInfo = mStore.name;
		mBasicInfoTextView.setText(basicInfo);
		
		String address = mStore.address;
		mAddressTextView.setText(address);
		
		String phoneNumber = mStore.phone;
		mPhoneNumberTextView.setText("电话: "+phoneNumber);
		
		String introduce = mStore.introduce;
		mIntroduceTextView.setText(introduce);
		
	}
	
	private void fillBtnGroup() {
		mServiceBtn.setOnClickListener(this);
		mGoThereBtn.setOnClickListener(this);
		mCommentBtn.setOnClickListener(this);
		mShareBtn.setOnClickListener(this);
	}
	
	private void refresh() {
		mComments.clear();
		int i = 0;
		for(; i<10; i++) {
			Comment c = new Comment();
			c.mContent = "那些花儿" + "\t" + i * 23423;
			c.mTime = System.currentTimeMillis() - i*10000;
			mComments.add(c);
		}
		
		mAdapter.notifyDataSetChanged();
	}
	
	private void onCommentBtnClick() {
		Intent intent = new Intent(this, AddCommentActivity.class);
		startActivity(intent);
		overridePendingTransition(R.anim.activity_slide_right_in, R.anim.activity_slide_left_out);
	}
	
	private void onServiceBtnClick() {
		Intent intent = new Intent(this, QrCodeScanActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		overridePendingTransition(R.anim.activity_slide_right_in, R.anim.activity_slide_left_out);
	}
	
	@Override
	public void onClick(View view) {
		int id = view.getId();
		switch(id) {
		case R.id.detail_service:
			onServiceBtnClick();
			break;
			
		case R.id.detail_go_there:
			onBackPressed();
			break;
			
		case R.id.detail_comment:
			onCommentBtnClick();
			break;
			
		case R.id.detail_share:
			//TODO
			break;
			
		default:
			break;
		}
		
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO
		
	}
	
	private static class ViewHolder {
		public TextView mTvContent;
		public TextView mTvTime;
		public Comment mComment;
	}
	
	private class CommentAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mComments.size();
		}

		@Override
		public Comment getItem(int position) {
			return mComments.get(position);
		}

		@Override
		public long getItemId(int position) {
			Comment c = getItem(position);
			return c.mId;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if(null == convertView) {
				convertView = LayoutInflater.from(DetailActivity.this).inflate(R.layout.layout_detail_comment_item, null);
				holder = new ViewHolder();
				holder.mTvContent = (TextView) convertView.findViewById(R.id.comment_content);
				holder.mTvTime = (TextView) convertView.findViewById(R.id.comment_date_time);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.mComment = getItem(position);
			holder.mTvContent.setText(holder.mComment.mContent);
			holder.mTvTime.setText(DateTimeUtil.getDateString(holder.mComment.mTime));
			return convertView;
		}
		
	}
}
